package com.lanou3g.code0418.dirty;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by dllo on 18/4/17.
 */


public class Utils {
    private ReentrantReadWriteLock lock   //重入读写锁,专门用来锁线程的
            = new ReentrantReadWriteLock();

    public void updataPerson(Person person, String name, int age) {
        lock.writeLock().lock(); //写锁关锁

        person.setName(name);
        /*
        //让系统沉睡1s,为了脏读效果更容易出现
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        */

        person.setAge(age);
        lock.writeLock().unlock(); //写锁打开

    }

    public void showPerson(Person person) {
        lock.readLock().lock();
        System.out.println("姓名:" + person.getName());
        System.out.println("年龄:" + person.getAge());
        lock.readLock().unlock();
    }

}
